import { defineStore, acceptHMRUpdate } from 'pinia'
import type { RouteRecordRawIcon } from '@/types/types.d'

export const useTagsViewStore = defineStore({
  id: 'tagsView',
  state: () => ({
    visitedViews: <RouteRecordRawIcon[]>[],
    cachedViews: <any[]>[],
  }),
  getters: {
  },
  actions: {
    addView(view: RouteRecordRawIcon | any) {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    addVisitedView(view: RouteRecordRawIcon) {
      if (this.visitedViews.some((v: RouteRecordRawIcon) => v.path === view.path)) return
      this.visitedViews.push(view)
    },
    addCachedView(view: RouteRecordRawIcon) {
      if (this.cachedViews.includes(view.name)) return
      if (!view.meta?.noCache) {
        this.cachedViews.push(view.name)
      }
    },

    delView(view: RouteRecordRawIcon) {
      return new Promise(resolve => {
        this.delVisitedView(view)
        this.delCachedView(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    delVisitedView(view: RouteRecordRawIcon) {
      for (const [i, v] of this.visitedViews.entries()) {
        if (v.path === view.path) {
          this.visitedViews.splice(i, 1)
          break
        }
      }
      return Promise.resolve([...this.visitedViews])
    },
    delCachedView(view: RouteRecordRawIcon) {
      const index = this.cachedViews.indexOf(view.name)
      index > -1 && this.cachedViews.splice(index, 1)
      return Promise.resolve([...this.cachedViews])
    },

    delOthersViews(view: RouteRecordRawIcon) {
      return new Promise(resolve => {
        this.delOthersVisitedViews(view)
        this.delOthersCachedViews(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    delOthersVisitedViews(view: RouteRecordRawIcon) {
      return new Promise(resolve => {
        this.visitedViews = this.visitedViews.filter(v => {
          return v.meta?.affix || v.path === view.path
        })
        resolve([...this.visitedViews])
      })
    },
    delOthersCachedViews(view: RouteRecordRawIcon) {
      return new Promise(resolve => {
        const index = this.cachedViews.indexOf(view.name)
        if (index > -1) {
          this.cachedViews = this.cachedViews.slice(index, index + 1)
        } else {
          // if index = -1, there is no cached tags
          this.cachedViews = []
        }
        resolve([...this.cachedViews])
      })
    },

    delAllViews() {
      return new Promise(resolve => {
        this.delAllVisitedViews()
        this.delAllCachedViews()
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    delAllVisitedViews() {
      return new Promise(resolve => {
        const affixTags = this.visitedViews.filter(tag => tag.meta?.affix)
        this.visitedViews = affixTags
        resolve([...this.visitedViews])
      })
    },
    delAllCachedViews() {
      return new Promise(resolve => {
        this.cachedViews = []
        resolve([...this.cachedViews])
      })
    },

    updateVisitedView(view: RouteRecordRawIcon | any) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view)
          break
        }
      }
    }
  },
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useTagsViewStore, import.meta.hot))
}